package com.groupbuying.storeinternal.mq;

import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * 商户中心消息消费MQ配置文件
 * @Author wenguoqiang
 * @Date 2019-05-10
 */
@Configuration
public class StoreInternalMQConsumer {

    @Autowired
    private StoreInternalResultListener storeResultListener;

    public static final Logger LOGGER = LoggerFactory.getLogger(StoreInternalMQConsumer.class);

    @Bean
    public Consumer getStoreInternalMQConsumer() {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.GROUP_ID, StoreInternalMQConfig.GROUP_ID);
        properties.put(PropertyKeyConst.AccessKey, StoreInternalMQConfig.ACCESS_KEY);
        properties.put(PropertyKeyConst.SecretKey, StoreInternalMQConfig.SECRET_KEY);
        properties.put(PropertyKeyConst.NAMESRV_ADDR, StoreInternalMQConfig.NAMESRV_ADDR);
        Consumer consumer = ONSFactory.createConsumer(properties);
        try {
            //订阅支付中心下所有支付回执的Tag
            consumer.subscribe(StoreInternalMQConfig.TOPIC, StoreInternalMQConfig.TAG_ALL ,storeResultListener);
            consumer.start();
            LOGGER.info("producer is start");
        } catch (Exception var4) {
            LOGGER.error(String.format("producer is error "));
        }

        return consumer;
    }
}
